﻿#include "VersionControlDescEx.h"

#include <E_VersionControl.nanopb.h>
#include <DescStore/VersionControlVersionControlDesc.h>
#include <NFLogicCommon/NFBaseDefine.h>

VersionControlDescEx::VersionControlDescEx()
{
    if (EN_OBJ_MODE_INIT == NFShmMgr::Instance()->GetCreateMode())
    {
        CreateInit();
    }
    else
    {
        ResumeInit();
    }
}

VersionControlDescEx::~VersionControlDescEx()
{
}

int VersionControlDescEx::CreateInit()
{
    return 0;
}

int VersionControlDescEx::ResumeInit()
{
    return 0;
}

int VersionControlDescEx::Load()
{
    return 0;
}

int VersionControlDescEx::ParseVersion(const std::string &versionStr, VersionDesc &stVersion)
{
    TClientVersion stVer = ParseClientVersion(versionStr);

    stVersion.big_ver = stVer.m_iBigVer;
    stVersion.mid_ver = stVer.m_iMidVer;
    stVersion.min_ver = stVer.m_iMinVer;
    stVersion.build_cnt = stVer.m_iBuildCnt;
    return 0;
}

int VersionControlDescEx::CheckWhenAllDataLoaded()
{
    int iRetCode = 0;
    for (auto iter = VersionControlVersionControlDesc::Instance()->GetResDesc().begin(); iter != VersionControlVersionControlDesc::Instance()->GetResDesc().end(); iter++)
    {
        auto &desc = *iter;
        CHECK_EXPR(IsValidPlatType(desc.platType), -1, "invalid plattype:{}", desc.id);

        iRetCode = ParseVersion(desc.minCliVer.ToString(), desc.min_cli_ver);
        CHECK_RET(iRetCode, "parse version:{} fail", desc.minCliVer.ToString());

        iRetCode = ParseVersion(desc.maxCliVer.ToString(), desc.max_cli_ver);
        CHECK_RET(iRetCode, "parse version:{} fail", desc.maxResVer.ToString());

        desc.is_max_cli_limit = true;
        if (desc.max_cli_ver.big_ver == 0 && desc.max_cli_ver.mid_ver == 0
            && desc.max_cli_ver.min_ver == 0 && desc.max_cli_ver.build_cnt == 0)
        {
            desc.is_max_cli_limit = false;
        }

        iRetCode = ParseVersion(desc.minResVer.ToString(), desc.min_res_ver);
        CHECK_RET(iRetCode, "parse version:{} failed", desc.minResVer.ToString());

        iRetCode = ParseVersion(desc.maxResVer.ToString(), desc.max_res_ver);
        CHECK_RET(iRetCode, "parse version:{} failed", desc.maxResVer.ToString());

        desc.is_max_res_limit = true;
        if (desc.max_res_ver.big_ver == 0 && desc.max_res_ver.mid_ver == 0
            && desc.max_res_ver.min_ver == 0 && desc.max_res_ver.build_cnt == 0)
        {
            desc.is_max_res_limit = false;
        }
    }
    return 0;
}

const E_VersionControlVersionControl *VersionControlDescEx::GetDescByWorldIdZoneIdPlatType(int64_t iWorldID, int64_t iZoneID, int64_t iPlatType)
{
    auto pDesc = VersionControlVersionControlDesc::Instance()->GetDescByWorldIDZoneIDPlatType(iWorldID, iZoneID, iPlatType);
    if (pDesc)
    {
        return pDesc;
    }

    pDesc = VersionControlVersionControlDesc::Instance()->GetDescByWorldIDZoneIDPlatType(iWorldID, 0, iPlatType);
    if (pDesc)
    {
        return pDesc;
    }

    pDesc = VersionControlVersionControlDesc::Instance()->GetDescByWorldIDZoneIDPlatType(0, 0, iPlatType);
    if (pDesc)
    {
        return pDesc;
    }

    return NULL;
}
